package com.citywy.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInteceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestURI=request.getRequestURI();
        System.out.println(requestURI);
        //如果是登陆页面则放行
        if(requestURI.indexOf("login.htm")>=0||requestURI.equals("/")||requestURI.indexOf("loginsubmit")>0){
            System.out.println("不进来？");
            return true;
        }
        HttpSession session = request.getSession();
        System.out.println("输出保存的活动中用户:"+session.getAttribute("activeUser"));
        //如果用户已登录放行
        if(session.getAttribute("activeUser")!=null){
            return true;
        }
        request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request,response);
        return false;
    }
}
